<!---
Copyright: (c) 2006 The MachBlog Authors
Authors: Matt Woodward (mpwoodward@gmail.com) & Peter J. Farrell (pjf@maestropublishing.com)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License. 
You may obtain a copy of the License at 

http://www.apache.org/licenses/LICENSE-2.0 

Unless required by applicable law or agreed to in writing, software 
distributed under the License is distributed on an "AS IS" BASIS, 
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
See the License for the specific language governing permissions and 
limitations under the License.

$Id: ExceptionListener.cfc 176 2006-01-30 14:40:17Z mpwoodward $

Notes:
--->
<cfcomponent
	displayname="sysListener"
	extends="MachII.framework.Listener"	
	output="false"
	hint="Performs exception listener actions.">

	<!---
	PROPERTIES
	--->
	<cfset variables.instance = StructNew() />
	<cfset variables.notificationService = "" />

	<!---
	INITIALIZATION / CONFIGURATION
	--->
	<cffunction name="configure" access="public" returntype="void" output="false"
		hint="Initializes the listener.">
		<cfset setDevMode(getProperty("devMode", FALSE)) />
	</cffunction>

	<cffunction name="setNotificationService" access="public" returntype="void" output="false"
		hint="Dependency: injected">
		<cfargument name="notificationService" type="machblog.org.machblog.notification.NotificationService" required="true" />
		<cfset variables.notificationService = arguments.notificationService />
	</cffunction>
	<cffunction name="getNotificationService" access="private" returntype="machblog.org.machblog.notification.NotificationService" output="false">
		<cfreturn variables.notificationService />
	</cffunction>

	<!---
	PUBLIC FUNCTIONS
	--->
	<cffunction name="doException" access="public" returntype="void" output="false"
		hint="Announces the correct event and sends email of exception if required.">
		<cfargument name="event" type="MachII.framework.Event" required="true" />

		<cfset var exception = arguments.event.getArg("exception", "") />
		<cfset var type = exception.getCaughtException().type />

		<cfif getDevMode()>
			<cfset announceEvent("development", arguments.event.getArgs()) />
		<cfelse>
			<cfswitch expression="#type#">
				<cfcase value="MachII.framework.EventHandlerNotDefined|MachII.framework.EventHandlerNotAccessible" delimiters="|">
					<cfset announceEvent("productionNoEventHandler", arguments.event.getArgs()) />
				</cfcase>
				<cfdefaultcase>
					<cfset getNotificationService().sendExceptionEmail(getProperty("appName"), exception, arguments.event.getArg("exceptionEvent").getArg("mainEvent", "")) />
					<cfset announceEvent("productionDefault", arguments.event.getArgs()) />
				</cfdefaultcase>
			</cfswitch>
		</cfif>
	</cffunction>

	<!---
	ACCESSORS
	--->
	<cffunction name="setDevMode" access="private" returntype="void" output="false">
		<cfargument name="devMode" required="true" type="string" />
		<cfset variables.instance.devMode = arguments.devMode />
	</cffunction>
	<cffunction name="getDevMode" access="private" returntype="string" output="false">
		<cfreturn variables.instance.devMode />
	</cffunction>

</cfcomponent>